package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.LivePersonBean;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 我的直播Adapter
 * Created by wjy on 2018/10/12.
 */
public class MyLiveListItemAdapter extends BaseListAdapter<LivePersonBean, MyLiveListItemAdapter.ViewHolder> {

    private boolean mRevert = true;
    private int mAllNumber;

    public MyLiveListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    public void setAllNumber(int number) {
        mAllNumber = number;
    }

    public void setRevert(boolean isRevert) {
        mRevert = isRevert;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_person_live_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        LivePersonBean comment = mData.get(position);
        if (position == 0) {
            holder.year.setVisibility(View.VISIBLE);
            holder.year.setText(getYear(comment.getBeginDate()) + "年");
        } else {
            holder.year.setVisibility(View.GONE);
        }

        holder.tv_comment.setText(comment.getTitle());
        holder.title_date.setText(getDate(comment.getBeginDate()));
        Glide.with(mContext).load(Constant.IMAGE_PREFIX+comment.getImgurl()).into(holder.image);

    }

    private String getYear(String time) {
        try {
            return time.substring(0, 4);
        } catch (Exception e) {
            return "2019";
        }
    }

    private String getDate(String time) {
        try {
            String date = time.split(" ")[0];
            String number[] = date.split("-");
            return number[1] + "月" + number[2] + "日";
        } catch (Exception e) {
            return "";
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, title_date, content, year, tv_comment;
        ImageView image;
        RoundedImageView head;

        public ViewHolder(View itemView) {
            super(itemView);
            year = itemView.findViewById(R.id.tv_year);
            title_date = itemView.findViewById(R.id.tv_date_title);
            image = itemView.findViewById(R.id.image);
            tv_comment = itemView.findViewById(R.id.tv_comment);
        }
    }


}
